<?php

/**
 * Controller_teaching
 * 教研计划进度表
 */

class Controller_teaching extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->user   	= YOU::loadModel('user');
		$this->model->file   	= YOU::loadModel('file');
		$this->model->grade   	= YOU::loadModel('grade');

	}
	/** 首页 **/
	public function actionIndex()
	{

	}

	/** ----------------------各种与同教学计划进度表 --相同-------------------------- **/
	/** 教学计划进度列表 **/
	public function actionPlanList ()
	{
		// 查询条件
		$grade_id		= $this->get->grade_id;
		$subject_id		= $this->get->subject_id;
		$from			= $this->get->from;
		$to				= $this->get->to;
		$manage_type	= (int)$this->get->manage_type;

		$where = "  ";

		if(!empty($grade_id)){

			$where .= " AND grade_id ='{$grade_id}' ";
			$this->tpl->assign("grade_id", $grade_id);
		}

		if(!empty($subject_id)){

			$where .= " AND subject_id ='{$subject_id}' ";
			$this->tpl->assign("subject_id", $subject_id);
		}

		if(!empty($from)){

			$where .= " AND create_time >= '{$from}' ";
			$this->tpl->assign("from", $from);
		}

		if(!empty($to)){

			$where .= " AND create_time <= '{$to}+1' ";
			$this->tpl->assign("to", $to);
		}

		// 全部年级
		$grade_lists 	= $this->model->grade->getAll();
		// 全部学科
		$subject_lists		= $this->db->fetchAll("SELECT `subject_name`,`subject_id` FROM `tbl_subject`");
		// 各分类名称
		$manage_name = $this->manage_name[$manage_type];
		// 分页
//		YOU::loadLib('page', "", '0');
//		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_manage` WHERE manage_type='{$manage_type}' {$where}");
//		$count = $tmp['count'];
//		$page_total = ceil($count/PAGE_LIMIT);
//		// 当前页
//		$p = intval($_GET['p']);
//		if ($p == 0){
//			$p  = 1;
//		}
//		if ($p > 1){
//			$start = PAGE_LIMIT * ($p-1);
//		}else{
//			$start = 0;
//		}

		$manage_lists = $this->db->fetchAll("SELECT * FROM `tbl_manage` WHERE manage_type='{$manage_type}' {$where} ORDER BY manage_id DESC ");

		foreach($manage_lists as $key=>$value){
		
			// 年级
			//$grade_data 	= $this->model->grade->getinfo($value['grade_id']);
			$grade_data = $this->db->fetchRow("SELECT `grade_name` FROM `tbl_grade` WHERE grade_id='{$value['grade_id']}'");
			$manage_lists[$key]['grade_name'] = $grade_data['grade_name'];

			// 学科
			$subject_data = $this->db->fetchRow("SELECT `subject_name` FROM `tbl_subject` WHERE subject_id='{$value['subject_id']}'");
			$manage_lists[$key]['subject_name'] = $subject_data['subject_name'];
			// 提交者
			$manage_lists[$key]['user_name'] = $this->model->user->getNameById($value['user_id']);

		}

//		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
//		$page->next_page   = '下一页>';
//		$page->pre_page    = '<上一页';
//		$page->style_path  = "static/page_style";
//
//		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("manage_lists", $manage_lists);
		$this->tpl->assign("grade_lists", $grade_lists);
		$this->tpl->assign("subject_lists", $subject_lists);

		$this->tpl->assign("manage_type", $manage_type);
		$this->tpl->assign("manage_name", $manage_name);
		$this->tpl->display('teaching/m.plan.list.tpl.php');
	}
	/** 教学计划进度添加 **/
	public function actionPlanAdd()
	{
		$manage_type	= (int)$this->get->manage_type;
		// 各分类名称
		$manage_name = $this->manage_name[$manage_type];
		// 全部年级
		$grade_list 	= $this->model->grade->getAll();
		// 全部学科
		$subject_list	= $this->db->fetchAll("SELECT `subject_name`,`subject_id` FROM `tbl_subject`");

		if ($_POST){

			$grade_id     			= $this->post->grade_id;
			$subject_id     		= $this->post->subject_id;
			$manage_title     	    = $this->post->manage_title;
			$manage_content     	= $this->post->manage_content;

			// 写入数据库
			$info = array(

			"grade_id"       		=> $grade_id,
			"subject_id"       		=> $subject_id,
			"manage_title"  	    => $manage_title,
			"manage_type"       	=> $manage_type,
			"manage_content"       	=> $manage_content,
			"user_id"       		=> $this->user['user_id'],
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);

			$this->db->do_insert("tbl_manage", $info);
			$manage_id = $this->db->insert_id();

			// 附件上传
			$file = $this->model->file->upload("manage_file", $manage_id, array("txt","ppt", "xls", "xlsx", "docs", "doc", "pdf", "jpg", "png", "gif", "zip", "rar"));

			if (!empty($file)){
				$info = array(
				"manage_file" => $file['fname'],
				);

				$this->db->do_update("tbl_manage", $info, "manage_id='{$manage_id}'");
			}

			Helper_Client::location("添加成功", "index.php?a=teaching.planlist&manage_type={$manage_type}");

		}

		$this->tpl->assign("grade_list", $grade_list);
		$this->tpl->assign("manage_name", $manage_name);
		$this->tpl->assign("subject_list", $subject_list);
		$this->tpl->display("teaching/m.plan.add.tpl.php");
	}
	/** 教学计划进度修改 **/
	public function actionPlanUpdate()
	{

		$manage_id		= (int)$this->get->manage_id;
		$manage_type	= (int)$this->get->manage_type;
		// 各分类名称
		$manage_name 	= $this->manage_name[$manage_type];
		// 全部年级
		$grade_list 	= $this->model->grade->getAll();
		// 全部学科
		$subject_list	= $this->db->fetchAll("SELECT `subject_name`,`subject_id` FROM `tbl_subject`");

        // 标题
        $manage_data	= $this->db->fetchRow("SELECT * FROM `tbl_manage` WHERE `manage_id`='{$manage_id}' ");

        if (empty($manage_data)){
            Helper_Client::alert("计划进度表不存在");
        }
        $uid = $this->user['user_id'];
        if($manage_data['user_id']  && ($uid !=$manage_data['user_id']) ){
            Helper_Client::alert("对不起无此权限");
            exit;
        }



		if ($_POST){

			$grade_id     			= $this->post->grade_id;
			$subject_id     		= $this->post->subject_id;
			$manage_title     	    = $this->post->manage_title;
			$manage_content     	= $this->post->manage_content;

			// 写入数据库
			$info = array(

			"grade_id"       		=> $grade_id,
			"subject_id"       		=> $subject_id,
			"manage_title"  	    => $manage_title,
			"manage_content"       	=> $manage_content,
			"create_time"           => date("Y-m-d H:i:s"),

			);

			$this->db->do_update("tbl_manage", $info, "manage_id='{$manage_id}'");
            // 附件上传
            if ($this->post->manage_file_re=='1'){
                // 附件上传
                $file = $this->model->file->upload("manage_file", $manage_id, array("txt","ppt", "xls", "xlsx", "docs", "doc", "pdf", "jpg", "png", "gif", "zip", "rar"));

                if (!empty($file)){
                    $info = array(
                        "manage_file" => $file['fname'],
                    );

                    $this->db->do_update("tbl_manage", $info, "manage_id='{$manage_id}'");
                }
            }

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=teaching.planlist&manage_type={$manage_type}");

		}

		$this->tpl->assign("manage_data", $manage_data);
		$this->tpl->assign("subject_list", $subject_list);
		$this->tpl->assign("grade_list", $grade_list);
		$this->tpl->assign("manage_name", $manage_name);

		$this->tpl->display("teaching/m.plan.update.tpl.php");
	}
	/** 教学计划进度删除 **/
	public function actionPlanDel()
	{
		$manage_id		= $this->get->manage_id;
		$manage_type	= (int)$this->get->manage_type;
        $manage_data	= $this->db->fetchRow("SELECT * FROM `tbl_manage` WHERE `manage_id`='{$manage_id}' ");

        if (empty($manage_data)){
            Helper_Client::alert("计划进度表不存在");
        }
        $uid = $this->user['user_id'];
        if($manage_data['user_id']  && ($uid !=$manage_data['user_id']) ){
            Helper_Client::alert("对不起无此权限");
            exit;
        }
		$this->db->query("DELETE  FROM `tbl_manage` WHERE manage_id='{$manage_id}'");
		// 跳转
		Helper_Client::location("删除成功", "index.php?a=teaching.planlist&manage_type={$manage_type}");
	}
	/** 教学计划进度详情页 **/
	public function actionDetails()
	{

		$manage_id		= $this->get->manage_id;
		$manage_type	= (int)$this->get->manage_type;
		$manage_data	= $this->db->fetchRow("SELECT * FROM `tbl_manage` WHERE `manage_id`='{$manage_id}' ");
		// 时间
		$manage_data['create_time'] = date('Y-m-d',strtotime($manage_data['create_time'])	);
		// 提交者
		$manage_data['user_name'] = $this->model->user->getNameById($manage_data['user_id']);
        $grade_data = $this->db->fetchRow("SELECT `grade_name` FROM `tbl_grade` WHERE grade_id='{$manage_data['grade_id']}'");
        $manage_data['grade_name'] = $grade_data['grade_name'];

        // 学科
        $subject_data = $this->db->fetchRow("SELECT `subject_name` FROM `tbl_subject` WHERE subject_id='{$manage_data['subject_id']}'");
        $manage_data['subject_name'] = $subject_data['subject_name'];

		$this->tpl->assign("manage_data", $manage_data);
		$this->tpl->assign("manage_type", $manage_type);
		$this->tpl->display("teaching/m.teaching.details.tpl.php");
	}

	/** 下载**/
	public function actionoutput(){
		$fname = $this->get->manage_file;		
		$fpath = UPLOAD_PATH."/manage_file/{$fname}";

		// 输出下载文件头s
		header("Content-Disposition: attachment; filename={$fname}");
		// 在线输出
		readfile($fpath);

	}
}

?>